package com.cn.kabalifeuser.ui.my.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.CustomRefreshListener;
import com.cn.kabalifeuser.config.LoginHelper;
import com.cn.kabalifeuser.config.RefreshHelper;
import com.cn.kabalifeuser.ui.my.bean.HelpDetailsBean;
import com.cn.kabalifeuser.ui.my.bean.HelpListBean;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.view.NotDataView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by 晓林 on 2020/9/10.
 * function: 帮助
 */
public class HelpListActivity extends IBaseActivity<HelpListActivityView, HelpListActivityPresenter> implements HelpListActivityView {

    @BindView(R.id.rv_notice)
    RecyclerView rv_notice;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;


    List<HelpListBean.ResultBean.DataBean> mList;
    ListAdapter mAdapter;
    private View notDataView;


    @Override
    protected HelpListActivityPresenter createPresenter() {
        return new HelpListActivityPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.recyclerview_refresh_bar;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("使用帮助");
        mList = new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(getMe());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        layoutManager.setSmoothScrollbarEnabled(true);
        layoutManager.setAutoMeasureEnabled(true);
        rv_notice.setLayoutManager(layoutManager);
        rv_notice.setNestedScrollingEnabled(false);
        //创建适配器
        mAdapter = new ListAdapter(R.layout.activity_help_item, mList);
        //给RecyclerView设置适配器
        rv_notice.setAdapter(mAdapter);
        notDataView = NotDataView.notDataView(this, rv_notice, refreshLayout);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //todo 跳转详情
                Bundle bundle = new Bundle();
                bundle.putInt("id", mAdapter.getItem(position).getId());
                $startActivity(HelpDetailsActivity.class, bundle);
            }
        });
        pagehttp = 1;
        getListData();
    }

    @Override
    public void initRefreshLayout() {
        RefreshHelper.initRefreshLayout(refreshLayout, new CustomRefreshListener() {
            @Override
            public void _onRefresh(RefreshLayout refreshLayout) {
                pagehttp = 1;
                getListData();
            }

            @Override
            public void _onLoadMore(RefreshLayout refreshLayout) {
                pagehttp++;
                if (null != mList) {
                    getListData();
                }
            }
        });
    }

    /**
     * 获取数据。刷新
     */
    public void getListData() {
        mPresenter.onHelpListData(LoginHelper.getLonginData().getResult(), pagehttp, pageNum);
    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onHelpListSuccess(HelpListBean bean) {
        RefreshHelper.finishRefresh(refreshLayout, pagehttp);
        if (pagehttp == 1) {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.replaceData(bean.getResult().getData());
            }
        } else {
            if (bean.getResult() != null && bean.getResult().getData().size() > 0) {
                mAdapter.addData(bean.getResult().getData());
            }

        }
    }

    @Override
    public void onHelpDetailsSuccess(HelpDetailsBean bean) {

    }


    class ListAdapter extends BaseQuickAdapter<HelpListBean.ResultBean.DataBean, BaseViewHolder> {


        public ListAdapter(int layoutResId, @Nullable List<HelpListBean.ResultBean.DataBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, final HelpListBean.ResultBean.DataBean item) {
//            GlideUtil.ImageLoad(this.mContext, item.getList_img(), helper.getView(R.id.list_img));
            helper.setText(R.id.tv_content, item.getTitle());
//                    .setText(R.id.tv_present_price, item.getPresent_price())
//            ;

        }

    }
}
